package com.xl.mananger;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

import com.xl.bean.BaseModel;
import com.xl.bean.ModelMapping;
/**
 * 
 * @author 深圳-夕落  2014-11-25
 * model的管理类
 */
public class ModelManager extends Manager{

	public static BaseModel inItModel(String className,HttpServletRequest request){
		BaseModel model = null;
		try 
		{ 
			model = (BaseModel)loadClass(className).newInstance();
	        Field[] fields = model.getClass().getDeclaredFields();		
			for(Field f: fields)
			{				
					f.setAccessible(true);
					f.set(model, request.getParameter(f.getName()));
					f.setAccessible(false);				
			}
		}catch(Exception e )
		{
			
			System.out.println("实体类实例化失败");	
		}
			return model;
		
	}
	
	
	public static BaseModel getModel(HttpServletRequest request) {
		String [] paths = MappingManager.getPaht(request);
        String className = paths[1];
        ModelMapping modelMapping  = MappingManager.getModelValue(className);
        BaseModel model = inItModel(modelMapping.getClassName(), request);
		return model;
	}
}
